<?php

error_reporting(E_ALL);

require_once('AP5L.php');
AP5L::install();

if (! isset($php)) {
    $php = 'php';
}
session_start();
if (isset($_REQUEST['d'])) {
    $_SESSION = array();
    echo 'toast';
    exit;
}
if (isset($_SESSION['cform']) && isset($_GET['image'])) {
    $_SESSION['cform'] -> getImage();
    exit;
}
echo '<?xml version="1.0" encoding="utf-8" ?>' . chr(10);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Rapid Form Test</title>
    <style type="text/css">
.cform_errmsg { color: #ff3333; }
.cform_headcol { text-align: right; width: 140px; }
.cform_tab { border-width: 2px; border-style: solid; }
.cform_req:after { content: "\A0 *"; color: #ff3333; } /* A0 is &nbsp; */
.cform_roweven { background-color: #EEEECC; }
.cform_rowhead { background-color: #EECCEE; }
.cform_rowodd { background-color: #EEEEDD; }
.cform_wrapper { background-color: #DDDDCC; float:left; }
th.cform_th {
    border: 1px;
    border-style: solid;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 90%;
    padding: 2px;
    text-align: right;
    vertical-align: top;
}
th.cform_title {
    border: 1px;
    border-style: solid;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 120%;
}
td.cform_td {
    border: 1px;
    border-style: solid;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 80%;
    padding: 2px;
}
td.cform_header {
    border: 1px;
    border-style: solid;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 90%;
    font-weight: bold;
    padding: 2px;
    text-align: center;
}

    </style>
</head>
<body>
<?php
if (! isset($_SESSION['cform']) || isset($_GET['fry'])) {
    //
    // Get the browser context so we can hack for IE, etc.
    //
    $browser = $_SERVER['HTTP_USER_AGENT'];
    $clientIeAny = strpos($browser, 'MSIE ') !== false;
    $clientIe7x = strpos($browser, 'MSIE 7.') !== false;
    //
    // Create the form
    //
    echo 'Creating new form object<br/>';
    $_SESSION['cform'] = new AP5L_Forms_Rapid_ByMail(
        new AP5L_Forms_UserVerifier_Basic()
    );
    $form = &$_SESSION['cform'];
    $view = &$form -> getView();
    $view -> setErrorWrapper('<br/>');
    $view -> setImagePath('RapidFormTest.'. $php . '?image');
    if ($clientIeAny) {
        $view -> setRequiredWrapper(
            '', '<span class="cform_errmsg">&nbsp;*</span>');
    }
    //
    // Set up routing
    //
    $form -> addRoute('Customer Service', 'alan.langford@abivia.com');
    $form -> addRoute('Web Site Developers', 'alan.langford@abivia.com');
    $form -> addRoute('Bob\'s < Mailbox', 'alan.langford@abivia.com');
    $form -> setServer(array('host' => 'smtp8.sympatico.ca'));
    $form -> setFrom('website@ambitonline.com');
    //
    // Add fields for testing
    //
    $form -> addField(AP5L_Forms_Field::factory('setpass', 'pass', 12, array(8, 12)));
    $form -> setMessage('pass.heading', 'Password (>= 8 and <= 12 characters)');
    $form -> setMessage('pass.bad', 'Passwords must be the same and be between 8 and 12 characters.');
    $form -> setMessage('pass_verify.heading', 'Re-Enter password');
    $form -> addField(AP5L_Forms_Field::factory('check', 'opt1'));
    $form -> setMessage('opt1.heading', 'Option one');
    $form -> setMessage('opt1.label', 'I want option one.');
    $form -> addField(AP5L_Forms_Field::factory('check', 'opt2'));
    $form -> setMessage('opt2.heading', 'Option two');
    $form -> setMessage('opt2.label', 'I want option two.');
    $opt3 = AP5L_Forms_Field::factory('check', 'opt3');
    $opt3 -> addChoice(1, 'Opt 3: List one');
    $opt3 -> addChoice(2, 'Opt 3: List two, which is < list three & > list two');
    $opt3 -> addChoice(3, 'Opt 3: List three');
    $form -> addField($opt3);
    $form -> setMessage('opt3.heading', 'And under option three...');
    $rad1 = &AP5L_Forms_Field::factory('radio', 'rad1');
    $rad1 -> addChoice(1, 'Rad1: one');
    $rad1 -> addChoice(2, 'Rad1: two, which is < list three & > list two');
    $rad1 -> addChoice(3, 'Rad1: List three');
    $form -> addField($rad1);
    $form -> setMessage('rad1.heading', 'Now tune your radio...');
    $form -> addField(AP5L_Forms_Field::factory('label', 'lab1'), 'This is a static label');
    $form -> setMessage('lab1.heading', 'A label:');
    $form -> addField(AP5L_Forms_Field::factory('header', 'head1'), 'This is a labeled header.');
    $form -> setMessage('head1.heading', 'A heading:');
    $head2 = &AP5L_Forms_Field::factory('header', 'head2');
    $head2 -> setLabelMode(0);
    $form -> addField($head2, 'This is an unlabeled header.');
    $head3 = &AP5L_Forms_Field::factory('header', 'head3');
    $head3 -> setLabelMode(-1);
    $form -> addField($head3, 'This is a full header.');
    // Now the hairy stuff
    // Create a container with three rows.
    $rowContainer = &AP5L_Forms_Field::factory('rows', 'basket', 3, 500);
    $rowContainer -> setLabelMode(-1);
    // Add a label, a text field, a checkbox, a hidden field.
    $rowContainer -> addField(AP5L_Forms_Field::factory('label', 'item'));
    $rowContainer -> addField(AP5L_Forms_Field::factory('text', 'itemtext', 4, 5));
    $rowContainer -> addField(AP5L_Forms_Field::factory('check', 'itemcheck'));
    $rowContainer -> addField(AP5L_Forms_Field::factory('hidden', 'itemhid'));
    $rowContainer -> setClass(array('cform_roweven', 'cform_rowodd'));
    $rowContainer -> labelClass = 'cform_rowhead';
    // Add the container to the form
    $form -> addField($rowContainer);
    $form -> preset('comment');
} else {
    $form = &$_SESSION['cform'];
}

if (isset($_GET['dump'])) {
    echo '<pre>', print_r($form), '</pre>';
    exit;
}
if (isset($_GET['unittest'])) {
    $form -> _fields[] = AP5L_Forms_Field::factory('text', 'grp3', 30, 30, 3);
    $form -> _fields[] = AP5L_Forms_Field::factory('text', 'grp3.1', 30, 30, 3);
    $form -> _fields[] = AP5L_Forms_Field::factory('text', 'grp2', 30, 30, 2);
    $form -> _fields[] = AP5L_Forms_Field::factory('text', 'grp2.1', 30, 30, 2);
    $form -> _arrange();
    foreach ($form -> _fields as $key => $field) {
        echo 'key=' . $key . ' fpos=' . $field -> _fpos . ' name=' . $field -> _name
            . ' sg=' . $field -> _sortGroup . '<br/>';
    }
    $form -> _fields[] = AP5L_Forms_Field::factory('text', 'grp4', 30, 30, 4);
    $form -> _fields[] = AP5L_Forms_Field::factory('text', 'grp4.1', 30, 30, 4);
    $form -> _arrange();
    foreach ($form -> _fields as $key => $field) {
        echo 'key=' . $key . ' fpos=' . $field -> _fpos . ' name=' . $field -> _name
            . ' sg=' . $field -> _sortGroup . '<br/>';
    }
    exit(0);
}

if (isset($_GET['style'])) {
    $form -> preset($_GET['style']);
}

$form -> process();
$form -> generate();
echo '<br/>Info:<br/>Include path: ' . get_include_path();
echo '<br/><pre>', print_r($_REQUEST), '</pre>';
if ($form -> isDone()) {
    echo '<br/><pre>', print_r($form -> getResults()), '</pre>';
}
?>
</body>
</html>